package Watch;

import TagSystemUpdate.TagSystemUpdater;
import DatabaseUpdate.DatabaseUpdater;

/**
 * 
 * @author Hadasa Dorfman
 *
 */
public class TaskManager
{
	//watcher object
	WatchService watcher;
	//database updater object
	DatabaseUpdater databaseUpdater;
	//tag system updater object
	TagSystemUpdater tagSystemUpdater;
	
	/**
	 * Constructor
	 * @param newWatcher
	 * @param dbUpdater
	 * @param tSUpdater
	 */
	public TaskManager(WatchService newWatcher, DatabaseUpdater dbUpdater, TagSystemUpdater tSUpdater) {
		watcher = newWatcher;
		databaseUpdater = dbUpdater;
		tagSystemUpdater = tSUpdater;
	}
	
	//start all the threads
	public void startAll()
	{
		//start watcher thread
		new Thread(new Runnable(){
			@Override
			public void run() {
				watcher.run();
			}			
		}).start();
		//start databaseUpdater thread
		new Thread(new Runnable(){
			@Override
			public void run() {
				databaseUpdater.run();
			}			
		}).start();
		//start databaseUpdater thread
		new Thread(new Runnable(){
			@Override
			public void run() {
				tagSystemUpdater.run();
			}			
		}).start();
	}
	
	//pause for watching
	public void pauseWatcher(){ watcher.pause(); }
	
	public void resumeWatcher(){
		synchronized (watcher) {
			watcher.notify();
			watcher.resume();
		}
	}
	
	//stop for all
	public void pauseAll()
	{ 
		pauseWatcher();
		databaseUpdater.pause();
		tagSystemUpdater.pause();
	}
}
